<template>
	<u-popup :show="show" closeable :round="10" mode="bottom" @close="close">
		<view class="popup">
			<view class="popup-hd">选择设备</view>
			<view class="popup-bd">
				<view class="devicelist">
					<view class="device-item flex align-center" @tap="navTo(item)" v-for="(item, index) in list" :key="index">
						<view class="imgbox">
							<image v-if="item.imgDeviceTypeUrl" :src="filterUrl + item.imgDeviceTypeUrl.split(';')[0]"
								mode="widthFix"></image>
						</view>
						<view class="flex-sub">
							<view class="name">{{ item.devName }}<span class="mac">{{item.mac|filterMac}}</span></view>
							<view>{{ item.devType }}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</u-popup>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				show: false,
				list: []
			};
		},
		computed: {
			...mapState(['devicelist'])
		},
		methods: {
			...mapMutations(['$uStore']),
			open(type) {
				this.show = true;
				console.log('type', type);
				if (type == 'BP2A') {
					this.list = this.devicelist.filter(item => item.deviceTypeCode.indexOf(type) > -1 || item.deviceTypeCode
						.indexOf('V3') > -1);

				} else {
					this.list = this.devicelist.filter(item => item.deviceTypeCode.indexOf(type) > -1);

				}
			},
			close() {
				this.show = false;
			},
			navTo(device) {
				let path = '';
				switch (device.deviceTypeCode) {
					case 'O2Max':
					case 'O2Ring': {
						path = '/pages/device/o2device';
						break;
					}
					case 'BP2A': {
						path = '/pages/device/bp2device';
						break;
					}
					case 'V3': {
						path = '/pages/device/v3device';
						break;
					}
					case 'F2Pro': {
						path = '/pages/device/f2device';
						break;
					}
					case 'ER': {
						path = '/pages/device/erdevice';
						break;
					}
				}
				if (path) {
					this.$Router.push({
						path: path + '?deviceType=' + device.deviceTypeCode + '&deviceMac=' + device.mac
					});
					this.close();
				}
			}
		},
		filters: {
			filterMac: function(value) {
				if(value){
				return	value.split('-')[value.split('-').length-1]
				}else{
					return ''
				}
			}
		}
	};
</script>

<style scoped lang="scss">
	.popup {
		background: #f2f2f2;

		.popup-hd {
			padding: 15px;
		}

		.popup-bd {
			max-height: 60vh;
			overflow: auto;
			padding: 0 20rpx;
		}

		.device-item {
			background: #fff;
			padding: 20upx 30upx;
			border-radius: 12rpx;
			margin-bottom: 20rpx;
			color: #666;

			.name {
				color: #333;
				font-size: 30upx;
				margin-bottom: 10upx;
			}
			.mac{
				color: #d5d5d5;
				font-size: 30upx;
				margin-left: 10px;
			}

			.imgbox {
				width: 140upx;
				height: 140upx;
				margin-right: 20upx;

				image {
					display: block;
					width: 100%;
					height: 100%;
				}
			}
		}
	}
</style>